import QtQuick 2.0

Rectangle {
    id: fkwidget

    width: targetWidth
    height: targetHeight
    state: "inv"

    signal show
    signal hide

    signal showed
    signal hidden

    onShow:{
        if(state==="inv"){
            if(stacker.topLevelWidget){
                cross=stacker.topLevelWidget
                z=cross.z+1
                cross.crossed=true
                cross.crossedBy=fkwidget
            }else{
                z=1
            }
            stacker.topLevelWidget=fkwidget
            visible=true
            state="vis"
        }
    }
    onShowed: {
        ready=true
    }
    onHide:{
        if(state==="vis"){
            ready=false
            state="inv"
            if(cross){
                cross.crossed=crossed
                cross.crossedBy=crossedBy
            }
            if(crossedBy){
                crossedBy.cross=cross
            }else{
                stacker.topLevelWidget=cross
            }
            cross=null
            crossedBy=null
            crossed=false
        }
    }
    onHidden: {
        visible=false
    }
    property bool showAnimation: true
    property bool hideAnimation: false
    property int animationDuration: 1000
    property double relativeInitialX: 0.5
    property double relativeInitialY: 0.5
    property int absoluteInitialX: 100
    property int absoluteInitialY: 100
    property double relativeCenterX: 0.5
    property double relativeCenterY: 0.5
    property int absoluteCenterX: 100
    property int absoluteCenterY: 100
    property double relativeWidth: -1.0
    property double relativeHeight: -1.0
    property int absoluteWidth: 200
    property int absoluteHeight: 200
    readonly property int targetWidth: relativeWidth<0 ? absoluteWidth : parent.width*relativeWidth
    readonly property int targetHeight: relativeHeight<0 ? absoluteHeight : parent.height*relativeHeight
    readonly property int targetCenterX: relativeCenterX<0 ? parent.width*0.5-absoluteCenterX : (relativeCenterX-0.5)*parent.width
    readonly property int targetCenterY: relativeCenterY<0 ? parent.height*0.5-absoluteCenterY : (relativeCenterY-0.5)*parent.height
    readonly property int targetInitialX: relativeInitialX<0 ? parent.width*0.5-absoluteInitialX : (relativeInitialX-0.5)*parent.width
    readonly property int targetInitialY: relativeInitialY<0 ? parent.height*0.5-absoluteInitialY : (relativeInitialY-0.5)*parent.height
    property int viewOffsetX
    property int viewOffsetY
    property bool ready:false
    property var cross: null
    property bool crossed: false
    property var crossedBy: null
    property var topLevelWidget:null
    property var stacker:parent
    border.width: 6
    border.color: "grey"
    anchors.centerIn: parent
    anchors.horizontalCenterOffset: viewOffsetX
    anchors.verticalCenterOffset: viewOffsetY
    clip: true
    color: "green"
    enabled: (ready && !crossed)
    states:[
        State{
            name: "vis"
            PropertyChanges{target: fkwidget; width: targetWidth;}
            PropertyChanges{target: fkwidget; height: targetHeight;}
            PropertyChanges{target: fkwidget; viewOffsetX: targetCenterX;}
            PropertyChanges{target: fkwidget; viewOffsetY: targetCenterY;}
            onCompleted:{
                fkwidget.showed()
            }
        },
        State{
            name: "inv"
            PropertyChanges{target: fkwidget; width: 0;}
            PropertyChanges{target: fkwidget; height: 0;}
            PropertyChanges{target: fkwidget; viewOffsetX: targetInitialX;}
            PropertyChanges{target: fkwidget; viewOffsetY: targetInitialY;}
            onCompleted:{
                fkwidget.hidden()
            }
        }
    ]
    transitions: [
        Transition {
            from: showAnimation ? hideAnimation ? "*" : "inv" : hideAnimation ? "vis" : ""
            to: "*"
            NumberAnimation { target: fkwidget; properties: "width"; duration: animationDuration; easing.type: Easing.Linear }
            NumberAnimation { target: fkwidget; properties: "height"; duration: animationDuration; easing.type: Easing.Linear }
            NumberAnimation { target: fkwidget; properties: "viewOffsetX"; duration: animationDuration; easing.type: Easing.Linear }
            NumberAnimation { target: fkwidget; properties: "viewOffsetY"; duration: animationDuration; easing.type: Easing.Linear }
        }
    ]
}
